package com.freecycle.sg.sqlite;

import android.net.Uri;
import android.provider.BaseColumns;

public class Freebie {
	
	public static final String CONTENT_AUTHORITY = "com.freecycle.sg";

    private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
    
    private static final String PATH_REFERENCES = "reference_data";
    private static final String PATH_USERS = "users";
    private static final String PATH_FREEBIES = "freebies";
    
	interface ReferenceDataColumns {
        String REF_DATA_TYPE = "ref_data_type";
        String ABBREVIATION = "abbreviation";
        String NAME = "name";
        String MARKER_URL = "marker_url";
	}
	
	interface UserColumns {
        String USER_ID = "user_id";
        String USER_PASSWORD = "password";
        String USER_NAME = "fullname";
        String USER_EMAIL = "email";
        String USER_PHONE = "phone";
        String LATITUDE_USER = "latitude_user";
        String LONGITUDE_USER = "longitude_user";
    }
	
	interface FreebiesColumns {
        String FREEBIE_ID = "freebie_id";
		String ADDED = "added";
		String LATITUDE = "latitude";
		String LONGITUDE = "longitude";
		String FORMATTED_ADDRESS = "formatted_address";
		String NAME = "name";
		String TITLE = "title";
		String DESCRIPTION = "description";
		String CATEGORY = "category";
		String PHONE = "phone";
	}
	
	/**
     * Reference data table.
     */
    public static class ReferenceData implements ReferenceDataColumns, BaseColumns {
        public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_REFERENCES).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/vnd.freecycle.sg.reference";
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/vnd.freecycle.sg.reference";

        /** Default "ORDER BY" clause. */
        public static final String DEFAULT_SORT = ReferenceDataColumns.ABBREVIATION + " COLLATE NOCASE ASC";

        /** Build {@link Uri} for requested {@link #REF_DATA_TYPE}. */
        public static Uri buildReferenceDataUri(String referenceType) {
            return CONTENT_URI.buildUpon().appendPath(referenceType).build();
        }

        /** Read {@link #REF_DATA_TYPE} from {@link ReferenceData} {@link Uri}. */
        public static String getReferenceType(Uri uri) {
        	return uri.getPathSegments().get(1);
        }
    }
	
	/**
     * Users table contains all users'information
     */
    public static class Users implements UserColumns, BaseColumns {
        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_USERS).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/vnd.freecycle.sg.users";
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/vnd.freecycle.sg.users";

        /** Default "ORDER BY" clause. */
		public static final String DEFAULT_SORT = UserColumns.USER_ID + " COLLATE NOCASE ASC";
        
        /** Build {@link Uri} for requested {@link #USER_ID}. */
        public static Uri buildUserUri(String userId) {
            return CONTENT_URI.buildUpon().appendPath(userId).build();
        }
        
        /** Read {@link #USER_ID} from {@link Users} {@link Uri}. */
        public static String getUserId(Uri uri) {
            return uri.getPathSegments().get(1);
        }

        /**
         * Build {@link Uri} that references any {@link Freebies} associated
         * with the requested {@link #USER_ID}.
         */
        public static Uri buildFreebiesUri(String userId) {
            return CONTENT_URI.buildUpon().appendPath(userId).appendPath(PATH_FREEBIES).build();
        }
    }
    
    /**
     * Freebies table contains all enquires sent to agents.
     */
    public static class Freebies implements FreebiesColumns, BaseColumns {

    	public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_FREEBIES).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/vnd.freecycle.sg.freebies";
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/vnd.freecycle.sg.freebies";

        /** Default "ORDER BY" clause. */
        public static final String DEFAULT_SORT = FreebiesColumns.CATEGORY + " COLLATE NOCASE DESC";
        
        /** Build {@link Uri} for requested {@link #FREEBIE_ID}. */
        public static Uri buildFreebieUri(String freebieId) {
        	return Uri.withAppendedPath(CONTENT_URI, freebieId);
        }
        
        /** Return {@link #FREEBIE_ID} for given {@link Uri} . */
        public static String getFreebieId(Uri uri) {
        	return uri.getPathSegments().get(1);
        }
        
    }
	
}